import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Index from '../views/index.vue'
import Menu from '../views/sys/Menu.vue'
import User from '../views/sys/User.vue'
import Role from '../views/sys/Role.vue'
import UserCenter from '../views/UserCenter.vue'
import axios from 'axios'
import store from '../store'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
	children:[
		{ 	
		  path: '/index',
		  name: 'Index',
		  component: Index,
		  meta:{
			  title:'首页'
		  }
		} ,
		// {
		// 	path:'/sys/users',
		// 	name:'SysUser',
		// 	component:User 
		// },
		// {
		// 	path:'/sys/role',
		// 	name:'SysRole',
		// 	component:Role 
		// },
		// {
		// 	path:'/sys/menu',
		// 	name:'SysMenu',
		// 	component:Menu 
		// },
		// {
		//   path: '/UserCenter',
		//   name: 'UserCenter',
		//   component: () => import(/* webpackChunkName: "about" */ '../views/UserCenter.vue')
		// } 
	]
  },
 
  {
    path: '/Login',
    name: 'Login',
	meta:{
		title:'登录'
	},
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
	
	axios.get('/sys/menu/nav',{
		headers:{
			Authorization:localStorage.getItem('token')
		}
	}).then(res=>{		
		store.commit('setMenulist',res.data.data.nav)
		store.commit('setPermlist',res.data.data.authoritys)
		res.data.data.nav.forEach(menus=>{
			menus.children.forEach(m=>{
				const s={
							name:m.name,
							path:m.path,			
							meta:{
										icon:m.icon,
										title:m.title
									},
							component: () => import(/* webpackChunkName: "about" */ '../views/sys/'+m.component+'.vue')}			
				if(router.hasRoute(m.name)==false){
					router.addRoute('Home',s)
					next({ ...to})
				}
				next()
				
			}
			)
		}
		)
		}
	)
	
	 
}
)


export default router
